<?php
/*
class Helper_LDAP{
	
	private $connection;
	public $root = "dc=st,dc=com";
	public $ldap_server = "ldap://ldap.szs.st.com";
	
	function __construct(){
		$this->connect();
	}
	
	function __destruct(){
		@ldap_close($this->connection);
	}
	
	function connect(){
		
		$connection = @ldap_connect($ldap_server);
		if(!$connection){ // if not connected to ldap server
	  	
			return false;
		}
		
		ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3);
		//echo "version 3<br>\n";

	  // echo "verification on '$ldap_server': ";
	   if(($bind=@ldap_bind($connection)) == false){
	  	 die($connection);
	     print "bind:__FAILED__<br>\n";
	     return false;
	   }
	   $this->connection = $connection;
	   return true;
	}
	
	
	private function getResourceIdByUsername($username){
		
		die($this->connection);
	   // search for user
	   if ($res_id = ldap_search($this->connection,$this->root,"uid=$username")) {
	     return false;
	   } else {
	     //print "failure: search in LDAP-tree failed<br>";
	   	 return $res_id;
	   }
	}
	
	function checkUserPassword($username,$password){
		$connection = $this->connection;
		
		$res_id = $this->getResourceIdByUsername($username);
		
		if (ldap_count_entries($connection, $res_id) != 1) {
	     	//print "failure: username $username found more than once<br>\n";
	    	 return false;
	    }
	    
	    if (( $entry_id = ldap_first_entry($connection, $res_id))== false) {
	     	return false;
	   	}
		
		if (( $user_dn = ldap_get_dn($connection, $entry_id)) == false) {
		 //print "failure: user-dn coulnd't be fetched<br>\n";
		 	return false;
		}
		
		// Authentifizierung des User 
		if (($link_id = @ldap_bind($connection, $user_dn, $password)) == false) {
		 	return false;
		} else {
			
			
			if($info = ldap_get_entries($connection,$res_id)){
				return $info;
			}
		}
		
		return false;	   	
	   	
	   	
	}
	
}
*/
class Helper_LDAP{
	function findUserByName($username){
		$ldap_server = "ldap.szs.st.com";
		$connect=@ldap_connect($ldap_server);
		
		if(!$connect) return false;
		ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
		if(($bind=@ldap_bind($connect)) == false){ return false;}
		
		if (($res_id = ldap_search( $connect,
			                   "dc=st,dc=com",
			                   "uid=$username")) == false) {
			return false;
		}
		
		if (ldap_count_entries($connect, $res_id) != 1) {
		
				return false;
		}	
			
			if (( $entry_id = ldap_first_entry($connect, $res_id))== false) {
			return false;
		}
			
		if($info = ldap_get_entries($connect,$res_id)){
				$ret['displayname']= $info[0]['displayname'][0];
				$ret['mail']= $info[0]['mail'][0];
				$ret['uid']= $info[0]['uid'][0];
				$ret['username']= $ret['uid'];
				$ret['employeenumber']= $info[0]['employeenumber'][0];				
				$ret['tina'] = $info[0]['telephonenumber'][0];
				$ret['mobile'] = $info[0]['mobile'][0];
				
				$ret['location'] = $info[0]['st-locationdescription'][0];
				
				@ldap_close($connect);
				return $ret;
			
		} else {
			return false;
		}
	}
	
	
	function checkUserPassword($username,$password,$ldap_server="ldap.szs.st.com"){
		if (empty($username) || empty($password)) {
			return false;
		}
		
		if($connect=@ldap_connect($ldap_server)){ // if connected to ldap server
			
			if (ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3)) {
				//echo "version 3<br>\n";
			} else {
				//echo "version 2<br>\n";
			}
			
			// echo "verification on '$ldap_server': ";
			
			// bind to ldap connection
			if(($bind=@ldap_bind($connect)) == false){
				//print "bind:__FAILED__<br>\n";
				return false;
			}
			
			// search for user
			if (($res_id = ldap_search( $connect,
			                   "dc=st,dc=com",
			                   "uid=$username")) == false) {
				//print "failure: search in LDAP-tree failed<br>";
				return false;
			}
			
			if (ldap_count_entries($connect, $res_id) != 1) {
				//print "failure: username $username found more than once<br>\n";
				return false;
			}
			
			if (( $entry_id = ldap_first_entry($connect, $res_id))== false) {
				//print "failur: entry of searchresult couln't be fetched<br>\n";
				return false;
			}
			
			if($info = ldap_get_entries($connect,$res_id)){
			
			
			}
			
			if (( $user_dn = ldap_get_dn($connect, $entry_id)) == false) {
				//print "failure: user-dn coulnd't be fetched<br>\n";
				return false;
			}
			/* Authentifizierung des User */
			if (($link_id = @ldap_bind($connect, $user_dn, $password)) == false) {
				//print "failure: username, password didn't match<br>\n";

				@ldap_close($connect);
				return false;
			}else{
				$ret['displayname']= $info[0]['displayname'][0];
				$ret['mail']= $info[0]['mail'][0];
				$ret['uid']= $info[0]['uid'][0];
				$ret['username']= $ret['uid'];
				$ret['employeenumber']= $info[0]['employeenumber'][0];
				
				@ldap_close($connect);
				return $ret;
			}
		} else {                                  // no conection to ldap server
			echo "no connection to '$ldap_server'<br>\n";
		}
		
		//echo "failed: ".ldap_error($connect)."<BR>\n";
		@ldap_close($connect);
		return false;
	
	}//end function checkldapuser
}